﻿import lite.mobile.lib.utils.Delegate;
import lite.mobile.lib.components.UButton;

/**
 * 因为考虑到12号宋体的显示,文本按钮的皮肤生成宽度必须 >= 30 左右.
 * @author libiao2011
 */
class lite.mobile.lib.components.UTextButton extends UButton {
	public function UTextButton(name : String ,depth : Number) {
		super(name, depth);
		//this.onCreateComplete = Delegate.create(obj, func) ;
	}

	public function getType() : String {
		return "UTextButton";
	}
	
	private var _textField : TextField ;
	private function initText() : Void {
		if(!_textField) {
			getCoreMC().createTextField("_textField", 1, 0, 0, 0, 0);
			_textField = getCoreMC()._textField ;
			_textField.multiline = false ;
			_textField.autoSize = "center" ;
			//_textField.border = true ;
			_textField.tabEnabled = false ;
			var tf : TextFormat = new TextFormat("宋体", 12);
			_textField.setNewTextFormat(tf);
		}
		trace("创建了文本: " + _textField);
	}
	
	public function setText(val : String) : Void {
		trace("我要设置~~~~~~~~");
		trace($_skinMC._width + "   " + $_skinMC._height);
		/*
		_textField.text = val ;
		
		_textField._width = $_skinMC._width ;
		_textField._height = _textField.textHeight + 4 ;
		 */

		_textField._visible = false ;
		_textField.text = "宽";
		var tw : Number = _textField.textWidth / 2 ;
		
		var len : Number = val.length ;
		var tn : Number = 0 ; 
		var ts : String = "";
		for(var i : Number = 0 ;i < len; i++) {
			// 默认加一个长度
			tn++ ;
			if(val.charCodeAt(i) > 127) {
				// 是汉字,再加一个长度
				tn++ ;
			}
			trace("wo: " + val.charCodeAt(i));
			// 比对值
			if(tw * (tn + 1) >= $_skinMC._width) {
				_textField.text = val.slice(0, i - 1) + "..." ;
				break ;
			}
		}
		/*
		for(var i:Number = 0 ; i < len ; i++){
		_textField.text = val.slice(0, i);
		trace("~~" + i + "  " + _textField.text);
		if(_textField.textWidth >= $_skinMC._width){
		_textField.text = val.slice(0, i-2) + "..." ;
		break ;
		}
		}
		 */
		_textField._x = ($_skinMC._width - _textField._width)*0.5 ;
		_textField._y = ($_skinMC._height - _textField._height)*0.5 ;
		
		_textField._visible = true ;
		trace("文本WH: " + _textField._width + "  " + _textField.textHeight);
		trace("文本TF-WH: " + _textField.textWidth + "  " + _textField.textHeight);
		trace("文本xy:  " + _textField._x);
	}

	public function getText() : String {
		return getTextField().text ;
	}
	
	public function getTextField() : TextField {
		return _textField ;
	}

	public function $_createComplete() : Void {
		trace("我生成完毕了...");
		initText();
		/*
		_textField._width = 100;
		_textField._height = 30;
		//
		_textField.textWidth = 100 ; //$_skinMC._width ;
		_textField.textHeight = 30; // $_skinMC._height ;
		//
		 */
		trace("修改设置~~~~~~~`");
	}
}
